$(function(){
	 // 调用商圈列表
	 selectAreaList();
	 //调用分类列表 
	 selectTypeList();
	 //商圈的切换 
	 var isSelect=null;
	 
})
//点击"全部分类"
function show(){
	$('#ceng').show()
}
//点击"取消"按钮
function cancal(){
	$('.twoClassify>li').removeClass("twoType");
	$('#ceng').hide();
}
//点击确定
function sumit(){
	var tijiao1=sessionStorage.getItem('postt').split(',');
	$('.twoClassify>li').removeClass("twoType");
	shangquanShop (tijiao1)
	 $('#ceng').hide()
 }
// 点击每一个商圈
function shangquanTab(that,shangquanId){
	// console.log(shangquanId)
	$('.item').removeClass("act1");
  $(that).addClass('act1');
	$('.xianshi>p').css('color','#666');
	shangquanShop (shangquanId)
}
//点击所有商圈
function allShangquan(id){
	 $('.item').removeClass("act1");
	 $('.xianshi>p').css('color','#ff9907');
 }
//点击每一行显示多少列 
function shulie(that){
	$('#section ul li>div').removeClass("active");
	$(that).addClass('active');
	 //列数内容的切换
	$(".main_center0").removeClass("show").addClass('hide');
	$('#' +$(that).attr('name') ).addClass('show').removeClass('hide');
	$(".main_center1").removeClass("show").addClass('hide');
	$('.' +$(that).attr('name') ).addClass('show').removeClass('hide');
}
//打开二级分类
function oneClassifyShow(that,id){
  var loginShow=false;
	 $('.img2').show();
	 $('.img1').hide();
	 var bele=sessionStorage.getItem('bale').split(',');
			for(var i=0;i<bele.length;i++){
				 if(('twoClasslist'+id)==bele[i]){
					 $('.twoClasslist'+id).toggle();
					 //$(".twoClasslist"+$('.twoClasslist').attr("name")).toggle();
				 }
			}	
 }
//二级分类实现多选
function TwoClassifySelect (that,id,oneId) {
	$('.quanFixrd_bottom>div:first-child').css({
		'color':'#fff',
		'background':'#ff9907'
	});
	if($(that).attr('name')=='trr'){
		$(that).attr("name","br");
		$(that).addClass('twoType');
	}else{
		$(that).attr("name","trr");
		$(that).removeClass('twoType');
	}
// 	if(oneID==oneId){
// 		$('.twoClassify>li').removeClass("twoType");
// 		for(var i=0;i<tijiao.length;i++){
// 			var postt=[];
// 			console.log(tijiao[i],id)
// 			postt.push(id)
// 			sessionStorage.setItem('postt',postt)
// 		}
// 	}
}
 //获取商圈列表
function  selectAreaList() {
	$.ajax({
		url: urlcore + '/api/tradeArea/selectList',
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(json) {			
			 var list=json.data;
			 if (json.data && json.success) {
				 var shangquanList='';
				 $.each(json.data,function(i,n){
					  shangquanList='<li class="item" name="'+n.id+'" onclick="shangquanTab(this,'+n.id+')">'+
									'<img src="'+n.areaUrl+'"/>'+n.areaName+'</li>';
					  $('.list').append(shangquanList)
						sessionStorage.setItem('id',n.id)
						//首次渲染 
            shangquanShop(n.id)
						//切换全部商圈
						 allShangquan(n.id)
				 })
					
			 }else{
				 alert(data.msg)
			 }
		},
		error:function(){
			alert(Error)
		}
	})
}	
// 获取商圈的商品
function shangquanShop (areaId,classifyId) {
	// console.log(classifyId)
	$('.margin_list').html('');
	$('.margin_list1').html('');
	$('.margin_list2').html('');
	var goodType="";
	var sortTypeIds="";
	$.ajax({
		url: urlcore + '/api/wantBuy/selectList?current=1&tradeAreaId=' + areaId + "&sortTypeId=" + goodType + "&sort=2&sortTypeIds"+classifyId,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(json) {
			// console.log(json)
			if (json.data && json.success) {
        var askToBuyList = json.data.list;
        for(let obj of askToBuyList){
          var timeStr = timezhuan(obj.uptDatetime)
          if (timeStr.split(" ").length > 1){
            obj.time2 = timeStr + "发布";
            obj.time3 = timeStr.split(" ")[0] + "发布";
            obj.time4 = timeStr.split(" ")[0] + "发布";
						var mainList='<div class="main_center_list">'+
								'<img src="'+obj.coverUrl+'" class="main_img"/>'+
								'<img src="images/add/qiugou_right.png" class="main_img1"/>'+
								'<p>'+obj.time2+'</p>'+
							'</div>';
						var mainList1='<div class="main_center_list2">'+
								'<img src="'+obj.coverUrl+'" class="main_img2"/>'+
								'<img src="images/add/qiugou_right.png" class="main_img3"/>'+
								'<p>'+obj.time3+'</p>'+
							'</div>';
						var mainList2='<div class="main_center_list3">'+
								'<img src="'+obj.coverUrl+'" class="main_img4"/>'+
								'<img src="images/add/qiugou_right.png" class="main_img5"/>'+
								'<p>'+obj.time4+'</p>'+
							'</div>';										
						$('.margin_list').append(mainList);
						$('.margin_list1').append(mainList1);
						$('.margin_list2').append(mainList2);
          }else{
            obj.time2 = timeStr;
            obj.time3 = timeStr;
            obj.time4 = timeStr;
          }
        }
		    }
		}
	})		
}
// 获取分类列表 
 function  selectTypeList() {
	$.ajax({
		url: urlcore + '/api/wantBuyType/selectWantBuyTypeTwoList',
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(json) {
			var list=json.data;
			//存数据
// 			var twoType=[];
// 			twoType.push(json.data.list)
// 			 sessionStorage.setItem('classifyList',twoType);
// 			 console.log(twoType);
// console.log(json.data.list)
			if (json.data&& json.success) {
				var oneClassifyList='';
			  var selsect=[];
				var ppost=[];
				$.each(json.data.list ,function(i,n){
					
					 oneClassifyList+='<div  class="li" onclick="oneClassifyShow(this,'+n.id+')">'+
								'<div class="oneclassify">'+n.typeName+'</div>'+
								'<div class="oneclassify_img">'+
									'<img src="images/add/xia.png" class="img1"/>'+
									'<img src="images/add/shang.png"  class="img2"/>'+
								'</div>'+
							'</div><div class="twoClasslist'+n.id+' twoClasslist" name="'+n.id+'"><ul class="twoClassify" data-id="'+n.id+'">';
						$.each(n.wantBuyTypeList,function(i,m){
							 oneClassifyList+='<li><div style="width:100%;height:100%;" name="trr" data-id="'+m.oneTypeId+'"  onclick="TwoClassifySelect(this,'+m.id+','+n.id+')">'+m.typeName+'</div></li>';
						   ppost.push(m.id)
							 sessionStorage.setItem('ppost',ppost)
						})  
						//存储class的类名 
						selsect.push('twoClasslist'+n.id)
						sessionStorage.setItem('bale',selsect)
						 
            oneClassifyList+='</ul></div>';	
				})
				$('.ol').append(oneClassifyList,)	
			}else{
				alert(data.msg)
			}
		},
		error:function(){
			alert(Error)
		}
	})
}
//点击发布 
function qiugoujia(){
	var tpken=sessionStorage.getItem('ryToken');
	if(tpken){
		window.location.href = "4.2.7qiugou_fabu.html";
	}
}
//时间转换 
function timezhuan(time) {
    var that = this;
    var nowTime = Date.parse(new Date());
    var subTime = (nowTime - Date.parse(new Date(time))) / 1000;
    var timeStr = '';
    if (subTime < 60) {
      timeStr = '刚刚发布';
    } else if (parseInt(subTime / 60) < 60) {
      timeStr = parseInt((subTime / 60)) + '分钟前发布';
    } else if (parseInt(subTime / (60 * 60)) < 24) {
      timeStr = parseInt(subTime / (60 * 60)) + '小时前发布';
    } else if (parseInt(subTime / (60 * 60 * 24)) < 2) {
      timeStr = '昨天' + time.split(" ")[1] +"发布";
    } else if (parseInt(subTime / (60 * 60 * 24)) < 3) {
      timeStr = '前天' + time.split(" ")[1] + "发布";
    } else {
      timeStr = time;
    }
    return timeStr;
  }
   